package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private QuestionService questionService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendService friendService;

    @DubboReference
    private CommentService commentService;

    // 打招呼（聊一下）
    public void liaoYiXia(Long jiaRenId, String reply) {

        // 1.拼接消息参数格式
        Map<String, String> param = new HashMap<>();
        param.put("userId", UserHolder.get().getId().toString()); // 登录用户id
        UserInfo userInfo = userInfoService.findById(UserHolder.get().getId());
        param.put("nickname", userInfo.getNickname()); // 登录用户昵称
        Question question = questionService.findByUserId(jiaRenId);
        if (question == null) {
            question = new Question();
        }
        param.put("strangerQuestion", question.getStrangerQuestion()); // 佳人陌生人问题
        param.put("reply", reply); // 你的回答

        // 转为json
        String json = JSON.toJSONString(param);

        // 2.向对方发送消息
        huanXinTemplate.sendMsg(jiaRenId.toString(), json);
    }

    // 添加好友
    public void addFriend(Long friendId) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.添加mongo好友
        friendService.addFriend(userId, friendId);
        // 3.添加环信好友
        huanXinTemplate.addContacts(userId, friendId);
    }

    // 查看好友列表
    public ResponseEntity findContactVoByPage(Integer pageNum, Integer pageSize) {
        // 1.获取线程内 userId
        Long userId = UserHolder.get().getId();

        // 2.调用rpc查询好友分页对象
        PageBeanVo pageBeanVo = friendService.findFriendByPage(userId, pageNum, pageSize);
        // 3.封装contactVo
        // 3-1 声明vo集合
        List<ContactVo> voList = new ArrayList<>();
        // 3-2 取出friend集合
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
        // 3-3遍历friend集合
        if (CollectionUtil.isNotEmpty(friendList)) {
            for (Friend friend : friendList) {
                // 创建好友的userInfo
                UserInfo friendUserInfo = userInfoService.findById(friend.getFriendId());

                // 创建vo
                ContactVo vo = new ContactVo();
                vo.setUserInfo(friendUserInfo); // 设置好友信息
                vo.setUserId(friend.getFriendId().toString()); // 设置好友id（很重要。。。）

                // 将vo添加到集合
                voList.add(vo);
            }
        }

        // 4.将vo设置到分页对象中
        pageBeanVo.setItems(voList);

        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 查看点赞、评论、喜欢人信息
    public ResponseEntity findByCommentType(Integer pageNum, Integer pageSize, Integer commentType) {
        // 1 .获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        PageBeanVo pageBeanVo = commentService.findUserCommentByPage(userId, pageNum, pageSize, commentType);

        // 3.封装userCommentVo
        // 3-1 声明vo集合
        List<UserCommentVo> voList = new ArrayList<>();
        // 3-2 取出comment集合
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        // 3-3 遍历
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 查询评论人的信息
                UserInfo userInfo = userInfoService.findById(comment.getUserId());

                // 创建vo
                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString());// 评论人id
                vo.setNickname(userInfo.getNickname()); // 评论人昵称
                vo.setAvatar(userInfo.getAvatar()); // 评论人头像
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 评论时间

                // 将vo设置到集合中
                voList.add(vo);

            }
        }

        // 4.将vo设置到分页对象
        pageBeanVo.setItems(voList);

        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }
}
